import router from '@/router'
import store from '@/store'
import tools from '@/utils/tools'
import NProgress from 'nprogress'
import { clapArr } from '@/utils/arrDispose'

const whiteList = ['/Login', '/signUp', '/findPassword', '/testApp', '/conferenceRoom']

router.beforeEach(async (to, from, next) => {
  NProgress.start()
  // 获取token
  const token = store.getters.token
  // 获取路由表
  const routes = [...router.options.routes]
  const staticPathArr = clapArr(routes, 'path')
  // 判断倒计时是否存在未清除的定时器 若有 则清除
  if (store.state.common.timer) {
    store.commit('common/clear_timer')
    store.commit('common/set_countDownText', '获取验证码')
  }
  // 判断是否直接输入路径跳转视频通话/会议室 没有房间ID等信息 禁止进入
  if ((to.path === '/videoCall' || to.path === '/conferenceRoom') && !store.state.conferenceRoom.cf_id) {
    next(from.path)
  }
  if (token) {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      if (staticPathArr.indexOf(to.path) < 0) {
        next('/Login')
      } else {
        if (tools.storage.getSignUpTemp()) {
          tools.storage.removeSignUpTemp()
          next('/login')
        }
        if (!store.state.user.userInfo.user_id) {
          store.dispatch('user/getUserInfo')
        }
        next()
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/Login')
    }
  }
  NProgress.done()
})
